package com.tempjob.app.controller;

import com.anwen.mongo.conditions.query.LambdaQueryChainWrapper;
import com.anwen.mongo.model.PageParam;
import com.anwen.mongo.model.PageResult;
import com.tempjob.app.dto.*;
import com.tempjob.app.security.annotation.UserLogAnnotation;
import com.tempjob.app.security.annotation.UserPassportSSO;
import com.tempjob.app.vo.JobApplyUserListVO;
import com.tempjob.app.vo.JobApplyVO;
import com.tempjob.app.vo.JobListVO;
import com.tempjob.app.vo.PublishedJobVO;
import com.tempjob.common.businesss.mongo.entity.*;
import com.tempjob.common.businesss.service.AddressService;
import com.tempjob.common.businesss.service.MongoBeanUtil;
import com.tempjob.common.context.UserContext;
import com.tempjob.common.context.UserContextManage;
import com.tempjob.common.exception.BusinessException;
import com.tempjob.common.model.PositionModel;
import com.tempjob.common.model.TempJobInfo;
import com.tempjob.common.model.WorkDayModel;
import com.tempjob.common.utils.CachedBeanCopier;
import com.tempjob.common.utils.DateUtilsEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hui
 * @Date 2024/12/6 12:48
 **/
@RequestMapping("/employer")
@RestController
@Api(tags = "雇主相关接口")
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
@UserPassportSSO
public class EmployerController {

    @Autowired
    private MongoBeanUtil mongoBeanUtil;

    @ApiOperation(response = JobListVO.class, value = "工作发布", notes = "")
    @PostMapping(value = "/publishJob")
    @Transactional
    @UserLogAnnotation(title = "雇主接口", action = "工作发布")
    public Object publishJob(@RequestBody PublishJobDTO dto) {
        UserContext userContext = UserContextManage.getContext();
        PublishUserInfo publishInfo = mongoBeanUtil.getPublishUserInfoService().lambdaQuery().eq(PublishUserInfo::getUserId, userContext.getId()).eq(PublishUserInfo::getStatus, 1).one();
        if (publishInfo == null) {
            throw new BusinessException("请先完善信息后重试");
        }
        CompanyInfo companyInfo = userContext.getCurrentType().equals(2) && !publishInfo.getType().equals(3) ? mongoBeanUtil.getCompanyInfoService().getById(publishInfo.getCompanyId()) : null;
        JobInfo jobInfo = new JobInfo();
        if (StringUtils.isNotBlank(dto.getId())) {
            jobInfo = mongoBeanUtil.getJobInfoService().getById(dto.getId());
            if (jobInfo == null || !jobInfo.getPublishUserId().equals(userContext.getId())) {
                throw new BusinessException("记录未找到");
            }
        }
        jobInfo.setJobStatus(dto.getJobStatus());
        jobInfo.setJobType(dto.getJobType());
        jobInfo.setJobCategory(dto.getJobCategory());
        jobInfo.setJobTitle(dto.getJobTitle());
        jobInfo.setJobContent(dto.getJobContent());
        jobInfo.setJobAddress(dto.getJobAddress());
        jobInfo.setContactPhone(dto.getContactPhone());
        jobInfo.setContactName(dto.getContactName());
        jobInfo.setEducation(dto.getEducation());
        jobInfo.setSkill(dto.getSkill());
        jobInfo.setAuditStatus(1);
        jobInfo.setExperience(dto.getExperience());
        jobInfo.setDescImages(dto.getDescImages());
        jobInfo.setWelfareTags(dto.getWelfareTags());
        jobInfo.setPublishUserId(userContext.getId());
        jobInfo.setWorkOnTime(dto.getWorkOnTime());
        jobInfo.setWorkOffTime(dto.getWorkOffTime());
        jobInfo.setPublishUserName(userContext.getRealName());
        jobInfo.setDeadline(dto.getDeadline());
        jobInfo.setLatitude(dto.getLatitude());
        jobInfo.setLongitude(dto.getLongitude());
        if (dto.getLongitude() != null && dto.getLatitude() != null) {
            PositionModel positionInfo = AddressService.getAddress(dto.getLongitude(), dto.getLatitude());
            if (positionInfo != null) {
                jobInfo.setProvince(positionInfo.getProvince());
                jobInfo.setCity(positionInfo.getCity());
                jobInfo.setDistrict(positionInfo.getDistrict());
                jobInfo.setTownship(positionInfo.getTownship());
                jobInfo.setPositionInfo(positionInfo);
            }
        }
        jobInfo.setPublishNature(publishInfo.getType().equals(1) ? 1 : publishInfo.getType().equals(2) ? 2 : 3);
        jobInfo.setCompanyId(publishInfo.getCompanyId());
        jobInfo.setCompanyName(publishInfo.getCompanyName());
        if (!jobInfo.getJobType().equals(1) && (dto.getWelfareTags() == null || dto.getWelfareTags().isEmpty())) {
            jobInfo.setWelfareTags(companyInfo.getCompanyWelfare());
        }
        jobInfo.setIco(companyInfo == null ? null : companyInfo.getIco());
        jobInfo.setUpdateTime(new Date());

        if (jobInfo.getJobType().equals(1)) {
            if (dto.getTempJobInfo() == null) {
                throw new BusinessException("零工信息不能为空");
            }
            TempJobInfo tempJobInfo = dto.getTempJobInfo();
            if (tempJobInfo.getWorkDayList() == null || tempJobInfo.getWorkDayList().isEmpty()) {
                throw new BusinessException("工作时间不能为空");
            }
            List<WorkDayModel> list = new ArrayList<>();
            for (String s : tempJobInfo.getWorkDayList()) {
                WorkDayModel workDayModel = new WorkDayModel(s, tempJobInfo.getHireTotal(), 0, tempJobInfo.getHireTotal(), 0);
                list.add(workDayModel);
            }
            list = list.stream().sorted(Comparator.comparing(WorkDayModel::getDay)).collect(Collectors.toList());
            tempJobInfo.setWorkDayHireList(list);
            jobInfo.setTempJobInfo(tempJobInfo);
        } else {
            if (dto.getFullTimeJob() == null) {
                throw new BusinessException("全职信息不能为空!");
            }
            jobInfo.setFullTimeJob(dto.getFullTimeJob());
        }
        mongoBeanUtil.getJobInfoService().saveOrUpdate(jobInfo);
        return true;
    }


//    @ApiOperation(response = JobApplyVO.class, value = "查询用户简历", notes = "")
//    @PostMapping(value = "/queryUserCurriculumList")
//    @UserLogAnnotation(title = "雇主接口", action = "查询用户简历")
//    public Object queryUserCurriculumList(@RequestBody QueryUserCurriculumDTO dto) {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        QueryWrapper<Object> countWrapper = new QueryWrapper<>();
//        queryWrapper.orderByAsc("_id");
//        if (dto.getAge() != null) {
//            queryWrapper.gte("birthday", DateUtilsEx.getBithDayByAge(dto.getAge()));
//            countWrapper.gte("birthday", DateUtilsEx.getBithDayByAge(dto.getAge()));
//        }
//        if (StringUtils.isNotBlank(dto.getSex())) {
//            queryWrapper.eq("sex", dto.getSex());
//            countWrapper.eq("sex", dto.getSex());
//        }
//        if (dto.getUserStatus() != null) {
//            queryWrapper.eq("user_status", dto.getUserStatus());
//            countWrapper.eq("user_status", dto.getUserStatus());
//        }
//        if (StringUtils.isNotBlank(dto.getEducation())) {
//            queryWrapper.eq("education", dto.getEducation());
//            countWrapper.eq("education", dto.getEducation());
//        }
//        if (dto.getHireStatus() != null) {
//            queryWrapper.eq("hire_status", dto.getHireStatus());
//            countWrapper.eq("hire_status", dto.getHireStatus());
//        }
//        if (dto.getWorkExpetrience() != null) {
//            queryWrapper.lte("start_work_time", DateUtilsEx.getBithDayByAge(dto.getWorkExpetrience()));
//            countWrapper.lte("start_work_time", DateUtilsEx.getBithDayByAge(dto.getWorkExpetrience()));
//        }
//        if (dto.getIntentionType() != null) {
//            queryWrapper.eq("intention_info.intention_type", dto.getIntentionType());
//            countWrapper.eq("intention_info.intention_type", dto.getIntentionType());
//        }
//        if (StringUtils.isNotBlank(dto.getWantWorkPlace())) {
//            queryWrapper.eq("intention_info.want_work_place", dto.getWantWorkPlace());
//            countWrapper.eq("intention_info.want_work_place", dto.getWantWorkPlace());
//        }
//        LambdaAggregateChainWrapper<UserInfo> userIdQuery = mongoBeanUtil.getUserInfoService().lambdaAggregate();
//        if (dto.getIntentionType() != null || StringUtils.isNotBlank(dto.getWantWorkPlace())) {
//            userIdQuery.custom(BasicDBObject.parse("{$lookup: {from: 'user_intention_info',localField: '_id',foreignField: 'user_id',as: 'intention_info'}}")).custom(BasicDBObject.parse("{$unwind: '$intention_info'}"));
//        }
//        List<UserInfo> countQuery = userIdQuery.match(countWrapper).custom(BasicDBObject.parse("{$group: {_id: null,total: { $sum: 1 }}}")).custom(BasicDBObject.parse("{$project: { _id: 0,user_status: '$total'}}")).list();
//        Integer count = countQuery == null || countQuery.isEmpty() ? 0 : countQuery.get(0).getUserStatus();
//        PageResult<TempJobUserListVO> pageResult = new PageResult<>();
//        pageResult.setPageSize(dto.getSize());
//        pageResult.setPageNum(dto.getPage());
//        pageResult.setTotalSize(count);
//        List<TempJobUserListVO> list = new ArrayList<>();
//        if (count > 0) {
//            LambdaAggregateChainWrapper<UserInfo> userListQuery = mongoBeanUtil.getUserInfoService().lambdaAggregate();
//            if (dto.getIntentionType() != null || StringUtils.isNotBlank(dto.getWantWorkPlace())) {
//                userListQuery.custom(BasicDBObject.parse("{$lookup: {from: 'user_intention_info',localField: 'id',foreignField: 'user_id',as: 'intention_info'}}")).custom(BasicDBObject.parse("{$unwind: '$intention_info'}"));
//            }
//            List<UserInfo> userIdInfos = userListQuery.match(queryWrapper).skip((pageResult.getPageNum() - 1) * pageResult.getPageSize()).limit(pageResult.getPageSize()).list();
//            List<String> userIds = userIdInfos.stream().map(UserInfo::getId).collect(Collectors.toList());
//            List<UserInfo> userInfos = mongoBeanUtil.getUserInfoService().getByIds(userIds);
//            for (UserInfo contentDatum : userIdInfos) {
//                TempJobUserListVO applyVO = new TempJobUserListVO();
//                UserInfo user = userInfos.stream().filter(i -> i.getId().equals(contentDatum.getId())).findFirst().orElse(null);
//                if (user == null) {
//                    continue;
//                }
//                CachedBeanCopier.copyObject(user, applyVO);
//                applyVO.setId(contentDatum.getId());
//                list.add(applyVO);
//            }
//        }
//        pageResult.setContentData(list);
//        return pageResult;
//    }


    @ApiOperation(response = JobApplyVO.class, value = "普通工作申请记录", notes = "")
    @PostMapping(value = "/normalJobApplyRecord")
    @UserLogAnnotation(title = "雇主接口", action = "普通工作申请记录")
    public Object normalJobApplyRecord(@RequestBody JobApplyDTO dto) {
        PageParam page = new PageParam(dto.getPage(), dto.getSize());
        UserContext userContext = UserContextManage.getContext();
        PageResult<PublishedJobVO> pageResult = new PageResult<>();
        CachedBeanCopier.copyObject(page, pageResult);
        LambdaQueryChainWrapper queryWrapper = mongoBeanUtil.getJobInfoService().lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByDesc("_id");
        }
        Set<String> applyJobIds = mongoBeanUtil.getJobApplyRecordService().lambdaQuery().ne(JobApplyRecord::getJobType, 1).eq(dto.getApplyStatus() != null, JobApplyRecord::getApplyStatus, dto.getApplyStatus())
                .projectDisplay(JobApplyRecord::getJobId).list().stream().map(JobApplyRecord::getJobId).collect(Collectors.toSet());
        if (applyJobIds == null || applyJobIds.isEmpty()) {
            pageResult.setContentData(new ArrayList<>());
            return pageResult;
        }
        queryWrapper.in("_id", applyJobIds);
        queryWrapper.eq("publish_user_id", userContext.getId());
        queryWrapper.ne("job_type", 1);
        PageResult<JobInfo> pageInfo = mongoBeanUtil.getJobInfoService().page(queryWrapper, page);
        CachedBeanCopier.copyObject(pageInfo, pageResult);
        List<PublishedJobVO> list = new ArrayList<>();
        if (!pageInfo.getContentData().isEmpty()) {
            List<String> jobIds = pageInfo.getContentData().stream().map(JobInfo::getId).collect(Collectors.toList());
            List<JobApplyRecord> applyList = mongoBeanUtil.getJobApplyRecordService().lambdaQuery().in(JobApplyRecord::getJobId, jobIds).ne(JobApplyRecord::getApplyStatus, 4).eq(dto.getApplyStatus() != null, JobApplyRecord::getApplyStatus, dto.getApplyStatus()).list();
            Set<String> userIds = applyList.stream().map(JobApplyRecord::getUserId).collect(Collectors.toSet());
            List<UserInfo> userInfos = userIds == null || userIds.isEmpty() ? new ArrayList<>() : mongoBeanUtil.getUserInfoService().getByIds(userIds);
            for (JobInfo contentDatum : pageInfo.getContentData()) {
                PublishedJobVO vo = new PublishedJobVO();
                CachedBeanCopier.copyObject(contentDatum, vo);
                vo.setDeadline(DateUtilsEx.getStringDateTime(contentDatum.getDeadline()));
                List<JobApplyUserListVO> applyUserInfos = new ArrayList<>();
                for (JobApplyRecord jobApplyRecord : applyList) {
                    if (jobApplyRecord.getJobId().equals(contentDatum.getId())) {
                        JobApplyUserListVO userListVO = new JobApplyUserListVO();
                        UserInfo user = userInfos.stream().filter(i -> i.getId().equals(jobApplyRecord.getUserId())).findFirst().orElse(null);
                        CachedBeanCopier.copyObject(user, userListVO);
                        userListVO.setId(jobApplyRecord.getId());
                        userListVO.setUserId(user.getId());
                        userListVO.setApplyStatus(jobApplyRecord.getApplyStatus());
                        userListVO.setBirthday(DateUtilsEx.getDay(user.getBirthday()));
                        userListVO.setApplyTime(DateUtilsEx.getStringDateTime(jobApplyRecord.getApplyTime()));
                        userListVO.setConfirmTime(DateUtilsEx.getStringDateTime(jobApplyRecord.getConfirmTime()));
                        applyUserInfos.add(userListVO);
                    }
                }
                vo.setApplyUserInfos(applyUserInfos);
                list.add(vo);
            }
        }
        pageResult.setContentData(list);
        return pageResult;
    }

    @ApiOperation(response = JobApplyUserListVO.class, value = "零工工作申请记录", notes = "")
    @PostMapping(value = "/tempJobApplyRecord")
    @UserLogAnnotation(title = "雇主接口", action = "零工工作申请记录")
    public Object tempJobApplyRecord(@RequestBody JobApplyDTO dto) {
        PageParam page = new PageParam(dto.getPage(), dto.getSize());
        UserContext userContext = UserContextManage.getContext();
        PageResult<PublishedJobVO> pageResult = new PageResult<>();
        CachedBeanCopier.copyObject(page, pageResult);
        LambdaQueryChainWrapper queryWrapper = mongoBeanUtil.getJobInfoService().lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByDesc("_id");
        }
        queryWrapper.eq("publish_user_id", userContext.getId());
        queryWrapper.eq("job_type", 1);
        Set<String> applyJobIds = mongoBeanUtil.getUserTempJobRecordService().lambdaQuery()//.eq(UserTempJobRecord::getJobType, 1)
                .eq(UserTempJobRecord::getApplyStatus, dto.getApplyStatus())
                .projectDisplay(UserTempJobRecord::getJobId).list().stream().map(UserTempJobRecord::getJobId).collect(Collectors.toSet());
        if (applyJobIds == null || applyJobIds.isEmpty()) {
            pageResult.setContentData(new ArrayList<>());
            return pageResult;
        }
        queryWrapper.in("_id", applyJobIds);
        PageResult<JobInfo> pageInfo = mongoBeanUtil.getJobInfoService().page(queryWrapper, page);
        CachedBeanCopier.copyObject(pageInfo, pageResult);
        List<PublishedJobVO> list = new ArrayList<>();
        if (!pageInfo.getContentData().isEmpty()) {
            List<String> jobIds = pageInfo.getContentData().stream().map(JobInfo::getId).collect(Collectors.toList());
            List<UserTempJobRecord> applyList = mongoBeanUtil.getUserTempJobRecordService().lambdaQuery().in(UserTempJobRecord::getJobId, jobIds).ne(UserTempJobRecord::getApplyStatus, 4).eq(dto.getApplyStatus() != null, UserTempJobRecord::getApplyStatus, dto.getApplyStatus()).list();
            Set<String> userIds = applyList.stream().map(UserTempJobRecord::getUserId).collect(Collectors.toSet());
            List<UserInfo> userInfos = userIds == null || userIds.isEmpty() ? new ArrayList<>() : mongoBeanUtil.getUserInfoService().getByIds(userIds);
            for (JobInfo contentDatum : pageInfo.getContentData()) {
                PublishedJobVO vo = new PublishedJobVO();
                CachedBeanCopier.copyObject(contentDatum, vo);
                vo.setDeadline(DateUtilsEx.getStringDateTime(contentDatum.getDeadline()));
                List<PublishedJobVO.TempApplyVO> applyUserInfos = new ArrayList<>();
                //这里把申请记录按天分类出来
                Map<String, List<UserTempJobRecord>> userApplys = applyList.stream().filter(i -> i.getJobId().equals(contentDatum.getId())).collect(Collectors.groupingBy(UserTempJobRecord::getWorkDate, Collectors.toList()));
                List<String> workDates = contentDatum.getTempJobInfo().getWorkDayHireList().stream().sorted(Comparator.comparing(WorkDayModel::getDay)).map(WorkDayModel::getDay).collect(Collectors.toList());
                for (String workDate : workDates) {
                    PublishedJobVO.TempApplyVO tempApplyVO = new PublishedJobVO.TempApplyVO();
                    tempApplyVO.setWorkDate(workDate);
                    WorkDayModel workDay = contentDatum.getTempJobInfo().getWorkDayHireList().stream().filter(i -> i.getDay().equals(workDate)).findFirst().orElse(null);
                    tempApplyVO.setHireTotal(workDay == null ? 0 : workDay.getWorkerNum());
                    tempApplyVO.setHiredNum(workDay == null ? 0 : workDay.getWorkerNumRecruited());
                    List<UserTempJobRecord> userList = userApplys.get(workDate);
                    List<JobApplyUserListVO> applyUserList = new ArrayList<>();
                    if (userList != null && !userList.isEmpty()) {
                        for (UserTempJobRecord userTempJobRecord : userList) {
                            JobApplyUserListVO userListVO = new JobApplyUserListVO();
                            UserInfo user = userInfos.stream().filter(i -> i.getId().equals(userTempJobRecord.getUserId())).findFirst().orElse(null);
                            CachedBeanCopier.copyObject(user, userListVO);
                            userListVO.setId(userTempJobRecord.getId());
                            userListVO.setUserId(user.getId());
                            userListVO.setApplyStatus(userTempJobRecord.getApplyStatus());
                            userListVO.setBirthday(DateUtilsEx.getDay(user.getBirthday()));
                            userListVO.setApplyTime(DateUtilsEx.getStringDateTime(userTempJobRecord.getApplyTime()));
                            userListVO.setConfirmTime(DateUtilsEx.getStringDateTime(userTempJobRecord.getConfirmTime()));
                            applyUserList.add(userListVO);
                        }
                    }
                    tempApplyVO.setApplyUserInfos(applyUserList);
                    applyUserInfos.add(tempApplyVO);
                }
                vo.setTempApplyUserInfos(applyUserInfos);
                list.add(vo);
            }
        }
        pageResult.setContentData(list);
        return pageResult;
    }


//    @ApiOperation(response = JobApplyVO.class, value = "普通工作申请记录", notes = "")
//    @PostMapping(value = "/normalJobApplyRecord")
//    @UserLogAnnotation(title = "雇主接口", action = "普通工作申请记录")
//    public Object normalJobApplyRecord(@RequestBody NormalJobApplyDTO dto) {
//        PageParam page = new PageParam(dto.getPage(), dto.getSize());
//        LambdaQueryChainWrapper queryWrapper = mongoBeanUtil.getJobInfoService().lambdaQuery();
//        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
//            if (dto.getSort().equalsIgnoreCase("asc")) {
//                queryWrapper.orderByAsc(dto.getOrder());
//            } else {
//                queryWrapper.orderByDesc(dto.getOrder());
//            }
//        } else {
//            queryWrapper.orderByDesc("apply_time");
//        }
//        JobInfo jobInfo = mongoBeanUtil.getJobInfoService().getById(dto.getJobId());
//        if (jobInfo == null || !jobInfo.getPublishUserId().equals(UserContextManage.getContext().getId())) {
//            throw new BusinessException("工作记录查询错误");
//        }
//        queryWrapper.eq("job_id", jobInfo.getId());
//        queryWrapper.eq(dto.getApplyStatus() != null, "apply_status", dto.getApplyStatus());
//        PageResult<JobApplyRecord> pageInfo = mongoBeanUtil.getJobApplyRecordService().page(queryWrapper, page);
//        PageResult<TempJobUserListVO> pageResult = new PageResult<>();
//        CachedBeanCopier.copyObject(pageInfo, pageResult);
//        if (!pageInfo.getContentData().isEmpty()) {
//            List<String> userIds = pageInfo.getContentData().stream().map(JobApplyRecord::getUserId).collect(Collectors.toList());
//            List<UserInfo> userInfos = mongoBeanUtil.getUserInfoService().getByIds(userIds);
//            List<TempJobUserListVO> list = new ArrayList<>();
//            for (JobApplyRecord contentDatum : pageInfo.getContentData()) {
//                TempJobUserListVO applyVO = new TempJobUserListVO();
//                UserInfo user = userInfos.stream().filter(i -> i.getId().equals(contentDatum.getUserId())).findFirst().orElse(null);
//                if (user == null) {
//                    continue;
//                }
//                CachedBeanCopier.copyObject(user, applyVO);
//                applyVO.setId(contentDatum.getId());
//                list.add(applyVO);
//            }
//            pageResult.setContentData(list);
//        }
//        return pageResult;
//    }

    @ApiOperation(response = Boolean.class, value = "一般工作申请审核", notes = "")
    @PostMapping(value = "/auditNormalApply")
    @UserLogAnnotation(title = "雇主接口", action = "一般工作申请审核")
    @Transactional
    public Object auditNormalApply(@RequestBody AuditTempUserApplyDTO dto) {
        JobApplyRecord applyRecord = mongoBeanUtil.getJobApplyRecordService().getById(dto.getId());
        if (applyRecord == null || !applyRecord.getApplyStatus().equals(1)) {
            throw new BusinessException("记录错误");
        }
        JobInfo jobInfo = mongoBeanUtil.getJobInfoService().getById(applyRecord.getJobId());
        if (jobInfo == null || !jobInfo.getPublishUserId().equals(UserContextManage.getContext().getId())) {
            throw new BusinessException("工作记录查询错误");
        }
        Integer hireTotal = jobInfo.getFullTimeJob().getHireTotal();
        Integer hiredNum = jobInfo.getFullTimeJob().getHiredNum() == null ? 0 : jobInfo.getFullTimeJob().getHiredNum();
        if (hiredNum.compareTo(hireTotal) >= 0) {
            throw new BusinessException("招聘人数已满");
        }
        jobInfo.getFullTimeJob().setHiredNum(hiredNum + 1);
        mongoBeanUtil.getJobInfoService().updateById(jobInfo);
        applyRecord.setApplyStatus(dto.getApplyStatus());
        mongoBeanUtil.getJobApplyRecordService().updateById(applyRecord);
        return true;
    }


    @ApiOperation(response = JobListVO.class, value = "我发布的工作列表", notes = "")
    @PostMapping(value = "/publishJobList")
    @Transactional
    @UserLogAnnotation(title = "雇主接口", action = "我发布的工作列表")
    public Object publishJobList(@RequestBody MyJobListDTO dto) {
        UserContext userContext = UserContextManage.getContext();
        PageParam page = new PageParam(dto.getPage(), dto.getSize());
        LambdaQueryChainWrapper queryWrapper = mongoBeanUtil.getJobInfoService().lambdaQuery();
        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
            if (dto.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(dto.getOrder());
            } else {
                queryWrapper.orderByDesc(dto.getOrder());
            }
        } else {
            queryWrapper.orderByDesc("_id");
        }
        queryWrapper.eq("publish_user_id", userContext.getId());
        queryWrapper.eq(dto.getJobType() != null, "job_type", dto.getJobType());
        queryWrapper.eq(dto.getJobStatus() != null, "job_status", dto.getJobStatus());
        PageResult<JobInfo> pageInfo = mongoBeanUtil.getJobInfoService().page(queryWrapper, page);
        return pageInfo;
    }

//    @ApiOperation(response = JobApplyUserListVO.class, value = "零工工作申请记录", notes = "")
//    @PostMapping(value = "/tempApplyRecord")
//    @UserLogAnnotation(title = "雇主接口", action = "零工工作申请记录")
//    public Object tempApplyRecord(@RequestBody QueryTempUserApplyDTO dto) {
//        PageParam page = new PageParam(dto.getPage(), dto.getSize());
//        LambdaQueryChainWrapper queryWrapper = mongoBeanUtil.getJobInfoService().lambdaQuery();
//        if (StringUtils.isNotBlank(dto.getOrder()) && StringUtils.isNotBlank(dto.getSort())) {
//            if (dto.getSort().equalsIgnoreCase("asc")) {
//                queryWrapper.orderByAsc(dto.getOrder());
//            } else {
//                queryWrapper.orderByDesc(dto.getOrder());
//            }
//        } else {
//            queryWrapper.orderByAsc("work_date");
//        }
//        JobInfo jobInfo = mongoBeanUtil.getJobInfoService().getById(dto.getJobId());
//        if (jobInfo == null || !jobInfo.getPublishUserId().equals(UserContextManage.getContext().getId())) {
//            throw new BusinessException("工作记录查询错误");
//        }
//        queryWrapper.eq("job_id", dto.getJobId());
//        queryWrapper.eq(StringUtils.isNotBlank(dto.getWorkDate()), "work_date", dto.getWorkDate());
//        queryWrapper.eq(dto.getApplyStatus() != null, "apply_status", dto.getApplyStatus());
//        PageResult<UserTempJobRecord> pageInfo = mongoBeanUtil.getUserTempJobRecordService().page(queryWrapper, page);
//        PageResult<JobApplyUserListVO> pageResult = new PageResult<>();
//        CachedBeanCopier.copyObject(pageInfo, pageResult);
//        if (!pageInfo.getContentData().isEmpty()) {
//            List<String> userIds = pageInfo.getContentData().stream().map(UserTempJobRecord::getUserId).collect(Collectors.toList());
//            List<UserInfo> userInfos = mongoBeanUtil.getUserInfoService().getByIds(userIds);
//            List<JobApplyUserListVO> list = new ArrayList<>();
//            for (UserTempJobRecord contentDatum : pageInfo.getContentData()) {
//                JobApplyUserListVO applyVO = new JobApplyUserListVO();
//                UserInfo user = userInfos.stream().filter(i -> i.getId().equals(contentDatum.getUserId())).findFirst().orElse(null);
//                if (user == null) {
//                    continue;
//                }
//                CachedBeanCopier.copyObject(user, applyVO);
//                applyVO.setId(contentDatum.getId());
//                list.add(applyVO);
//            }
//            pageResult.setContentData(list);
//        }
//        return pageResult;
//    }


    @ApiOperation(response = Boolean.class, value = "零工申请审核", notes = "")
    @PostMapping(value = "/auditTempApply")
    @UserLogAnnotation(title = "雇主接口", action = "零工申请审核")
    @Transactional
    public Object auditAempApply(@RequestBody AuditTempUserApplyDTO dto) {
        UserTempJobRecord applyRecord = mongoBeanUtil.getUserTempJobRecordService().getById(dto.getId());
        if (applyRecord == null || !applyRecord.getApplyStatus().equals(1)) {
            throw new BusinessException("记录错误");
        }
        JobInfo jobInfo = mongoBeanUtil.getJobInfoService().getById(applyRecord.getJobId());
        if (jobInfo == null || !jobInfo.getPublishUserId().equals(UserContextManage.getContext().getId())) {
            throw new BusinessException("工作记录查询错误");
        }
        if (dto.getApplyStatus().equals(3)) {
            List<WorkDayModel> tempInfo = jobInfo.getTempJobInfo().getWorkDayHireList();
            WorkDayModel workDay = tempInfo.stream().filter(i -> i.getDay().equals(applyRecord.getWorkDate())).findFirst().orElse(null);
            if (workDay.getWorkerNumRecruited() >= workDay.getWorkerNum()) {
                throw new BusinessException("当日报名已满");
            }
            workDay.setWorkerNumRecruited(workDay.getWorkerNumRecruited() + 1);
            mongoBeanUtil.getJobInfoService().updateById(jobInfo);
        }
        applyRecord.setApplyStatus(dto.getApplyStatus());
        mongoBeanUtil.getUserTempJobRecordService().updateById(applyRecord);
        return true;
    }


}
